<?php
/**
 * BaseModel.class.php
 * 基类模型
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-05-12
 */

namespace Admin\Model;
use Think\Model;

class BaseModel extends Model {
	//声明错误信息
	public 	$errMsg 	= false;

	/**
	 * 字段解析
	 * @param 	$type
	 * @param 	$info
	 * @param 	$isList
	 * @return 	$info
	 */
	protected function _analyField($type, $info, $isList = false){
		$roleText 	= FUserModel::ROLE_MAP;
		switch($type){
			//活动图片
			case 'activity_picture':
				if($isList){
					foreach($info as &$item){
						$item['path']	= C('SERVER_CDN').$item['path'];
						$item['sim_activity_title']	= cut_str($item['activity_title'], 10);
						$item['beautify_path']	= C('SERVER_CDN').$item['beautify_path'];
						$item['create_date']	= date('Y-m-d', $item['create_time']);
					}
				}else{
					$info['sim_activity_title']	= cut_str($info['activity_title'], 10);
					$info['path']	= C('SERVER_CDN').$info['path'];
					$info['beautify_path']	= C('SERVER_CDN').$info['beautify_path'];
					$info['create_date']	= date('Y-m-d', $info['create_time']);
				}
			break;
			//分红信息
			case 'deduct':
				$goodsType 	= [1 => '购买课程', 2 => '问答', 3 => '用户提现', 4 => '购物车', 5 => '会员购买'];
				if($isList){
					foreach($info as &$item){
						$item['create_date']	= date('Y-m-d', $item['create_time']);
						$item['goods_type_text']	= $goodsType[$item['goods_type']];
						$item['role_text']	= $roleText[$item['role_id']];
						$item['profit']	= $item['amount'] - $item['deduct'];
						if($item['goods_type'] == 5){
							$vipInfo 	= C('VIP_TYPE.'.$item['goods_id']);
							$item['goods_name']	 = $vipInfo['date_desc'];
							$item['price']	= $vipInfo['amount'];
						}
					}
				}else{
					$info['create_date']	= date('Y-m-d', $info['create_time']);
					$info['goods_type_text']	= $goodsType[$info['goods_type']];
					$info['role_text']	= $roleText[$info['role_id']];
					$info['profit']	= $info['amount'] - $info['deduct'];
				}
			break;
			//用户信息字段
			case 'fuser':
				$sexText 	= [1 => '男', 2 => '女', 0 => '未知'];
				if($isList){
					foreach($info as &$item){
						$item['sex_text']	= $roleText[$item['sex']];
						$item['role_text']	= $roleText[$item['role_id']];
						$item['subscribe_date']	= date('Y-m-d', $item['subscribe_time']);
						$item['vip_enddate']	= date('Y-m-d', $item['vip_endtime']);
					}
				}else{
					$info['sex_text']	= $roleText[$info['sex']];
					$info['role_text']	= $roleText[$info['role_id']];
					$info['subscribe_date']	= date('Y-m-d', $info['subscribe_time']);
					$info['vip_enddate']	= date('Y-m-d', $info['vip_endtime']);
				}
			break;
			//课程字段解析额
			case 'course':
				$status 	= [1 => '正常', 0 => '下架', -1 => '删除'];
				$buyType 	= [1 => '现金', 2 => '积分', 3 => '现金+积分'];
				$cate 	= [0 => '未知', 1 => '必修', 2 => '选修'];
				if($isList){
					foreach($info as &$item){
						$item['create_date']	= date('Y-m-d', $item['create_time']);
						$item['sim_name']	= msubstr($item['name'], 0, 10);
						$item['sim_decipt']	= msubstr($item['decipt'], 0, 10);
						$item['status_text']	= $status[$item['status']];
						$item['buy_type_text']	= $buyType[$item['buy_type']];
						$item['banner']	= C('SERVER_NAME').$item['banner'];
						$item['cate_text']	= $cate[$item['cate']];
					}
				}else{
					$info['create_date']	= date('Y-m-d', $info['create_time']);
					$info['sim_name']	= msubstr($info['name'], 0, 10);
					$info['sim_decipt']	= msubstr($info['decipt'], 0, 10);
					$info['status_text']	= $status[$info['status']];
					$info['buy_type_text']	= $buyType[$info['buy_type']];
					$info['banner']	= C('SERVER_NAME').$info['banner'];
					$info['cate_text']	= $cate[$info['cate']];
				}
			break;
			//活动字段解析
			case 'activity':
				$status 	= [1 => '正常', -1 => '删除'];
				$level 	= [1 => '小班', 2 => '中班', 3 => '大班'];
				if($isList){
					foreach($info as &$item){
						$item['create_date'] 	= date('Y-m-d', $item['create_time']);
						$item['update_date']	= date('Y-m-d', $item['update_time']);
						$item['banner']	= C('SERVER_NAME').$item['banner'];
						$item['status_text']	= $status[$item['status']];
						$item['level_text']	= $level[$item['level']];
					}
				}else{
					$info['create_date'] 	= date('Y-m-d', $info['create_time']);
					$info['update_date']	= date('Y-m-d', $info['update_time']);
					$info['banner']	= C('SERVER_NAME').$info['banner'];
					$info['status_text'] 	= $status[$item['status']];
					$info['level_text']	= $level[$info['level']];
				}
			break;
			//留言字段解析
			case 'leavemsg':
				$status 	= [0 => '待审核', 1 => '已通过', 2 => '未通过', -1 => '删除'];
				$relateType 	= [1 => '课程留言', 2 => '活动留言'];
				if($isList){
					foreach($info as &$item){
						$item['create_date']	= date('Y-m-d', $item['create_time']);
						$item['reply_date']	= date('Y-m-d', $item['reply_time']);
						$item['status_text']	= $status[$item['status']];
						$item['relate_type_text']	= $relateType[$item['relate_type']];
						$item['sim_content']	= cut_str($item['content'], 5);
						$item['update_date']	= date('Y-m-d', $item['update_time']);
					}
				}else{
					$info['create_date']	= date('Y-m-d', $info['create_time']);
					$info['reply_date']		= date('Y-m-d', $info['reply_time']);
					$info['status_text']	= $status[$info['status']];
					$info['relate_type_text']	= $relateType[$info['relate_type']];
					$info['sim_content']	= cut_str($info['content'], 5);
					$info['update_date']	= date('Y-m-d', $info['update_time']);
				}
			break;
			//活动须知字段解析
			case 'activityknown':
				$status 	= [1 => '正常', -1 => '删除'];
				if($isList){
					foreach($info as &$item){
						$item['create_date']	= date('Y-m-d', $item['create_time']);
						$item['update_date']	= date('Y-m-d', $item['update_time']);
						$item['audio']		= C('SERVER_NAME').$item['audio'];
						$item['status_text']	= $status[$item['status']];
					}
				}else{
					$info['create_date']	= date('Y-m-d', $info['create_time']);
					$info['update_date']	= date('Y-m-d', $info['update_time']);
					$info['audio']		= C('SERVER_NAME').$info['audio'];
					$info['status_text']	= $status[$info['status']];
				}
			break;
			//幸福语录字段解析
			case 'motto':
				$status 	= [1 => '正常', -1 => '删除', 2 => '待上线'];
				$resolveColumn 	= function(&$item) use ($status){
					$item['sim_title']	= cut_str($item['title'], 5);
					$item['create_date']	= date('Y-m-d', $item['create_time']);
					if($item['online_time'])
						$item['online_date']	= date('Y-m-d H:i:s', $item['online_time']);
					$item['update_date']	= date('Y-m-d', $item['update_time']);
					$item['picture']		= C('SERVER_CDN').$item['picture'];
					$item['audio']		= C('SERVER_CDN').$item['audio'];
					$item['status_text']	= $status[$item['status']];
				};
				if($isList){
					foreach($info as &$item){
						$resolveColumn($item);
					}
				}else{
					$resolveColumn($info);
				}
			break;
			//前台字段解析
			case 'frontuser':
				$status 	= [1 => '关注中', 0 => '未关注'];
				$sexText 	= [0 => '未知', 1 => '男', 2 => '女'];
				$childLevel 	= [0 => '未知', 1 => '小班', 2 => '中班', 3 => '大班'];
				if($isList){
					foreach($info as &$item){
						$item['sex_text']	= $sexText[$item['sex']];
						$item['vip_enddate']	= date('Y-m-d', $item['vip_endtime']);
						$item['status_text']	= $status[$item['status']];
						$item['child_level_text']	= $childLevel[$item['child_level']];
						$item['subscribe_date']	= date('Y-m-d', $item['subscribe_time']);
						$item['role_text']	= $roleText[$item['role_id']];
						if($item['child_birthday'])
							$item['child_birthday_date']	= date('Y-m-d', $item['child_birthday']);
 					}
				}else{
					$info['sex_text']	= $sex[$info['sex']];
					$info['vip_enddate']	= date('Y-m-d', $info['vip_endtime']);
					$info['status_text']	= $status[$info['status']];
					$info['child_level_text']	= $childLevel[$info['child_level']];
					$info['subscribe_date']	= date('Y-m-d', $info['subscribe_time']);
					$info['role_text']	= $roleText[$info['role_id']];
					if($info['child_birthday'])
						$info['child_birthday_date']	= date('Y-m-d', $item['child_birthday']);
				}
			break;
		}

		return $info;
	}
}